<template>
  <div class="CMmanagement">
    <h4>医柜锁商品管理</h4>
    <div class="ele-keyseach">
      <div class="left">
        <el-form :model="BindForm">
          <el-form-item label="RFID编号:">
            <el-col :span="12">
              <el-input
                v-model="BindForm.RFIDCode"
                placeholder="输入编号"
                class="ele-width"
              />
            </el-col>
          </el-form-item>
          <el-form-item label="商品:">
            <el-col :span="15">
              <select-goods
                ref="selectGoods"
                v-model="BindForm.commdityId"
                style="width:280px"
              />
            </el-col>
          </el-form-item>
          <el-form-item label="状态:">
            <el-col :span="12">
              <el-select
                v-model="Bindata.state"
                placeholder="状态"
                class="ele-width"
              >
                <el-option value="all" label="全部" />
                <el-option value="up" label="上线" />
                <el-option value="low" label="下线" />
                <el-option value="using" label="使用中" />
                <el-option value="defending" label="维护中" />
                <el-option value="sold" label="已销售" />
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="margin-left:100px;width:100px"
              @click="inititalInfo"
            >筛选</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="right">
        <p>医柜锁编号:</p>
        <p>{{ $route.params.rows ? $route.params.rows.rfid : '' }}</p>
      </div>
    </div>
    <div class="ele-custom">
      <el-radio-group v-model="Bindata.state">
        <el-radio-button label="all">全部</el-radio-button>
        <el-radio-button label="up">上线</el-radio-button>
        <el-radio-button label="low">下线</el-radio-button>
        <el-radio-button label="defending">维护中</el-radio-button>
        <el-radio-button label="using">使用中</el-radio-button>
        <el-radio-button label="sold">以销售</el-radio-button>
      </el-radio-group>
    </div>
    <div class="cus-table">
      <el-table
        ref="tables"
        v-loading="loading"
        :data="Bindata"
        :header-cell-style="headerRowStyle"
        @selection-change="handelSelection"
      >
        <el-table-column type="selection" />
        <el-table-column label="商品编号" prop="goodsSn" align="center" />
        <el-table-column label="商品" prop="goodsName" align="center" />
        <el-table-column label="规格" prop="specifications" align="center" />
        <el-table-column label="类型" prop="goodsType" align="center" />
        <el-table-column label="上线时间" prop="onlineTime" align="center" />
        <el-table-column label="状态" prop="stat" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.stat == '0'" type="success">上线</el-tag>
            <el-tag v-if="scope.row.stat == '1'" type="info">下线</el-tag>
            <el-tag v-if="scope.row.stat == '2'" type="primary">维护中</el-tag>
            <el-tag v-if="scope.row.stat == '3'" type="primary">使用中</el-tag>
            <el-tag v-if="scope.row.stat == '4'" type="primary">已销售</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          header-align="right"
          align="right"
          width="240"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.stat == '0'" class="Operation">
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handelLow([scope.row.goodsId])"
              >下线</el-button>
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handel_szwh([scope.row.goodsId])"
              >设置维护</el-button>
            </div>
            <div v-if="scope.row.stat == '1'" class="Operation">
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handelUp([scope.row.goodsId])"
              >上线</el-button>
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handel_szwh([scope.row.goodsId])"
              >设置维护</el-button>
            </div>
            <div v-if="scope.row.stat == '2'" class="Operation">
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handelLow([scope.row.goodsId])"
              >下线</el-button>
              <el-button
                type="parmary"
                plain
                size="mini"
                @click="handelUp([scope.row.goodsId])"
              >维护中</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="ele-footer-pagination">
      <div class="left">
        <el-button
          plain
          @click="$refs.tables.toggleAllSelection()"
        >全选</el-button>
        <el-button plain @click="handelRowLow">下线</el-button>
        <el-button plain @click="handelRowUp">上线</el-button>
      </div>
      <div class="right">
        <el-pagination
          :current-page="BindForm.page"
          :page-sizes="[10, 25, 35, 45]"
          :page-size="BindForm.limit"
          layout="total,sizes,prev,pager,next,jumper"
          :total="pages.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 设置维护 -->
    <el-dialog
      :visible.sync="visible_szwh"
      title="设置维护"
      class="ele-dialog-1"
      width="400px"
    >
      <el-form ref="forms" :model="params" :rules="pattern">
        <el-form-item prop="reason" label="原因">
          <el-input
            v-model="params.reason"
            :autosize="{ minRows: 7 }"
            type="textarea"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="ele-submit"
            @click="handeSubmit"
          >提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
/** 医柜锁商品管理 */
import SelectGoods from '@/components/SelectGoods/index'
import { RFIDCommodityList, changeRFIDCommodityState } from '@/api/lockItem'
export default {
  name: 'CMmanagement',
  components: {
    SelectGoods
  },
  data() {
    return {
      visible_szwh: false,
      loading: false,
      params: {
        reason: ''
      },
      pattern: {
        reason: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      BindForm: {
        commdityId: '',
        RFIDCode: this.$route.params.rows ? this.$route.params.rows.rfid : '',
        state: 'all',
        address: null,
        page: 1,
        limit: 10
      },
      Bindata: [{ test: '111' }],
      pages: {
        total: 0
      },
      selection: []
    }
  },
  activated() {
    this.inititalInfo()
  },
  mounted() {
    this.inititalInfo()
  },
  methods: {
    handelSelection(v) {
      this.selection = v.map(inf => inf.goodsId)
    },
    /** 提交设置维护 */
    handeSubmit() {
      this.$refs.forms.validate(v => {
        if (v) {
          this.changeRFIDCommodityState(this.goodsId, '0')
        }
      })
    },
    /** 单行下线 */
    handelLow(goodsId) {
      this.$confirm('你确定要下线吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(result => {
        this.changeRFIDCommodityState(goodsId, '1')
      })
    },
    /** 单行上线 */
    handelUp(goodsId) {
      this.changeRFIDCommodityState(goodsId, '0')
    },
    /** 设置维护 */
    handel_szwh(goodsId) {
      this.goodsId = goodsId
      this.visible_szwh = true
    },
    /** 多行上线 */
    handelRowUp() {
      if (this.selection.length > 0) {
        this.changeRFIDCommodityState(this.selection, '0')
      } else {
        this.$message.warning('请先选择数据')
      }
    },
    /** 多行下线 */
    handelRowLow() {
      if (this.selection.length > 0) {
        this.$confirm('你确定要下线吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(result => {
            this.changeRFIDCommodityState(this.selection, '1')
          })
          .catch(err => {
            this.$message.warning(err)
          })
      } else {
        this.$message.warning('请先选择数据')
      }
    },
    /** 初始化 */
    inititalInfo() {
      this.loading = true
      RFIDCommodityList(this.BindForm).then(result => {
        this.Bindata = result.data.data.data
        this.pages.total = result.data.data.total
        this.loading = false
      })
    },
    headerRowStyle({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#f3f3f3'
      } else {
        return ''
      }
    },
    handleSizeChange(size) {
      this.BindForm.limit = size
    },
    handleCurrentChange(index) {
      this.BindForm.page = index
    },
    /** 更新RFID商品上下架、维护状态 */
    changeRFIDCommodityState(id, type) {
      // 0	// 上线
      // 1	// 下线
      // 2	// 维护中
      // 3	// 使用中
      // 4	// 已经销售
      changeRFIDCommodityState({
        type: type,
        reason: this.params.reason || '',
        idList: id
      }).then(result => {
        this.$message.success('成功')
        this.inititalInfo()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.CMmanagement {
  padding: 50px 100px;
  .ele-keyseach {
    padding: 20px 40px;
    background: #f3f3f3;
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      ::v-deep.el-form {
        .el-form-item__label {
          width: 100px;
          text-align: start;
        }
        .ele-width {
          width: 280px;
        }
      }
    }
    .right {
      width: 150px;
      height: 150px;
      background: white;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      p {
        margin: 0;
      }
      p:first-child {
        font-size: 14px;
      }
      p:last-child {
        font-size: 2rem;
        font-weight: 500;
        margin-top: 10px;
      }
    }
  }
  .ele-custom {
    margin-top: 40px;
  }
  .cus-table {
    margin-top: 30px;
    .Operation {
      .el-button {
        width: 80px;
      }
    }
  }
  .ele-footer-pagination {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 40px;
    .left {
      width: 330px;
      .el-button {
        width: 100px;
      }
    }
  }
}
</style>
